package edu.usm.roberge.model;

import java.text.DecimalFormat;
import java.util.HashSet;
import java.util.Set;

/**
 * Data model of application
 * Keeps track of stock quote data retrieved from web service
 * Also contains some computational methods to calculate somethings
 * based on the list of current stock data
 */
public class StockStore {
	Set<StockQuoteData> stockQuotes = new HashSet<StockQuoteData>();
	StockStoreListener listener;
	DecimalFormat formatter = new DecimalFormat("$#,###,###.##");
	
	public interface StockStoreListener {
		void onStoreContentChanged(StockQuoteData data);
	}
	
	public StockStore(StockStoreListener listener) {
		this.listener = listener;
	}

	public synchronized void setStockQuoteData(StockQuoteData data) {
		if (stockQuotes.contains(data)) {
			stockQuotes.remove(data);
		}
		stockQuotes.add(data);
		listener.onStoreContentChanged(data);
	}
	
	private synchronized Set<StockQuoteData> getCopyOfStockData() {
		return new HashSet<StockQuoteData>(stockQuotes);
	}
	
	public static class PickedStock {
		String symbol, numberOfShares, profit;

		public String getSymbol() {
			return symbol;
		}

		public String getNumberOfShares() {
			return numberOfShares;
		}

		public String getProfit() {
			return profit;
		}
	}
	
	public PickedStock pickStockByMostOwned(double investmentAmount) {
		Set<StockQuoteData> stocks = getCopyOfStockData();
		int mostShares = -1;
		
		StockQuoteData selectedStock = null;
		for(StockQuoteData data : stocks) {
			int totalShares = calcAmountOfShares(investmentAmount, data.getLast());
			if (totalShares > mostShares) {
				mostShares = totalShares;
				selectedStock = data;
			}
		}
		
		return createPickedStock(mostShares, selectedStock);
	}

	private int calcAmountOfShares(double investmentAmount, String lastPrice) {
		double amountPerShare = Double.parseDouble(lastPrice);
		int totalShares = (int)(investmentAmount / amountPerShare);
		return totalShares;
	}

	private PickedStock createPickedStock(int mostShares, StockQuoteData selectedStock) {
		PickedStock pickedStock = new PickedStock();
		pickedStock.symbol = selectedStock.getSymbol();
		pickedStock.numberOfShares = "" + mostShares;
		pickedStock.profit = formatter.format(calcProfit(selectedStock.getOpen(), selectedStock.getLast(), mostShares));
		
		return pickedStock;
	}

	private double calcProfit(String open, String last, int mostShares) {
		if (open.equals("N/A")) {
			return -1D;
		}
		double openPrice = Double.parseDouble(open);
		double lastPrice = Double.parseDouble(last);
		double difference = lastPrice - openPrice;
		return (mostShares * difference);
	}
	
	public PickedStock pickStockByMostProfit(double investmentAmount) {
		Set<StockQuoteData> stocks = getCopyOfStockData();
		int shares = -1;
		double mostProfit = 0D;
		StockQuoteData selectedStock = null;
		
		for(StockQuoteData data : stocks) {
			int availableShares = calcAmountOfShares(investmentAmount, data.getLast());
			if (availableShares > 0) {
				double profit = calcProfit(data.getOpen(), data.getLast(), availableShares);
				if (profit > mostProfit) {
					mostProfit = profit;
					shares = availableShares;
					selectedStock = data;
				}
			}
		}
		
		return createPickedStock(shares, selectedStock);		
	}
}
